| Conditions | 1 |
| Paths | 2 |
| Total Lines | 342 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | define(['leaflet', 'rbush', 'helper', 'moment'], |
||
| 2 | function (L, rbush, helper, moment) { |
||
| 3 | 'use strict'; |
||
| 4 | |||
| 5 | var groupOnline; |
||
| 6 | var groupOffline; |
||
| 7 | var groupNew; |
||
| 8 | var groupLost; |
||
| 9 | var groupLines; |
||
| 10 | |||
| 11 | var labelLocations = [['left', 'middle', 0 / 8], |
||
| 12 | ['center', 'top', 6 / 8], |
||
| 13 | ['right', 'middle', 4 / 8], |
||
| 14 | ['left', 'top', 7 / 8], |
||
| 15 | ['left', 'ideographic', 1 / 8], |
||
| 16 | ['right', 'top', 5 / 8], |
||
| 17 | ['center', 'ideographic', 2 / 8], |
||
| 18 | ['right', 'ideographic', 3 / 8]]; |
||
| 19 | var labelShadow; |
||
| 20 | var bodyStyle = { fontFamily: 'sans-serif' }; |
||
| 21 | var nodeRadius = 4; |
||
| 22 | |||
| 23 | var cFont = document.createElement('canvas').getContext('2d'); |
||
| 24 | |||
| 25 | function measureText(font, text) { |
||
| 26 | cFont.font = font; |
||
| 27 | return cFont.measureText(text); |
||
| 28 | } |
||
| 29 | |||
| 30 | function mapRTree(d) { |
||
| 31 | return { minX: d.position.lat, minY: d.position.lng, maxX: d.position.lat, maxY: d.position.lng, label: d }; |
||
| 32 | } |
||
| 33 | |||
| 34 | function prepareLabel(fillStyle, fontSize, offset, stroke) { |
||
| 35 | return function (d) { |
||
| 36 | var font = fontSize + 'px ' + bodyStyle.fontFamily; |
||
| 37 | return { |
||
| 38 | position: L.latLng(d.nodeinfo.location.latitude, d.nodeinfo.location.longitude), |
||
| 39 | label: d.nodeinfo.hostname, |
||
| 40 | offset: offset, |
||
| 41 | fillStyle: fillStyle, |
||
| 42 | height: fontSize * 1.2, |
||
| 43 | font: font, |
||
| 44 | stroke: stroke, |
||
| 45 | width: measureText(font, d.nodeinfo.hostname).width |
||
| 46 | }; |
||
| 47 | }; |
||
| 48 | } |
||
| 49 | |||
| 50 | function calcOffset(offset, loc) { |
||
| 51 | return [offset * Math.cos(loc[2] * 2 * Math.PI), |
||
| 52 | -offset * Math.sin(loc[2] * 2 * Math.PI)]; |
||
| 53 | } |
||
| 54 | |||
| 55 | function labelRect(p, offset, anchor, label, minZoom, maxZoom, z) { |
||
| 56 | var margin = 1 + 1.41 * (1 - (z - minZoom) / (maxZoom - minZoom)); |
||
| 57 | |||
| 58 | var width = label.width * margin; |
||
| 59 | var height = label.height * margin; |
||
| 60 | |||
| 61 | var dx = { |
||
| 62 | left: 0, |
||
| 63 | right: -width, |
||
| 64 | center: -width / 2 |
||
| 65 | }; |
||
| 66 | |||
| 67 | var dy = { |
||
| 68 | top: 0, |
||
| 69 | ideographic: -height, |
||
| 70 | middle: -height / 2 |
||
| 71 | }; |
||
| 72 | |||
| 73 | var x = p.x + offset[0] + dx[anchor[0]]; |
||
| 74 | var y = p.y + offset[1] + dy[anchor[1]]; |
||
| 75 | |||
| 76 | return { minX: x, minY: y, maxX: x + width, maxY: y + height }; |
||
| 77 | } |
||
| 78 | |||
| 79 | function mkMarker(dict, iconFunc, router) { |
||
| 80 | return function (d) { |
||
| 81 | var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d)); |
||
| 82 | |||
| 83 | m.resetStyle = function resetStyle() { |
||
| 84 | m.setStyle(iconFunc(d)); |
||
| 85 | }; |
||
| 86 | |||
| 87 | m.on('click', function () { |
||
| 88 | router.fullUrl({ node: d.nodeinfo.node_id }); |
||
| 89 | }); |
||
| 90 | m.bindTooltip(d.nodeinfo.hostname); |
||
| 91 | |||
| 92 | dict[d.nodeinfo.node_id] = m; |
||
| 93 | |||
| 94 | return m; |
||
| 95 | }; |
||
| 96 | } |
||
| 97 | |||
| 98 | function addLinksToMap(dict, linkScale, graph, router) { |
||
| 99 | graph = graph.filter(function (d) { |
||
| 100 | return 'distance' in d && !d.vpn; |
||
| 101 | }); |
||
| 102 | |||
| 103 | return graph.map(function (d) { |
||
| 104 | var opts = { |
||
| 105 | color: linkScale(1 / d.tq), |
||
| 106 | weight: 4, |
||
| 107 | opacity: 0.5, |
||
| 108 | dashArray: 'none' |
||
| 109 | }; |
||
| 110 | |||
| 111 | var line = L.polyline(d.latlngs, opts); |
||
| 112 | |||
| 113 | line.resetStyle = function resetStyle() { |
||
| 114 | line.setStyle(opts); |
||
| 115 | }; |
||
| 116 | |||
| 117 | line.bindTooltip(d.source.node.nodeinfo.hostname + ' – ' + d.target.node.nodeinfo.hostname + '<br><strong>' + helper.showDistance(d) + ' / ' + helper.showTq(d) + '</strong>'); |
||
| 118 | line.on('click', function () { |
||
| 119 | router.fullUrl({ link: d.id }); |
||
| 120 | }); |
||
| 121 | |||
| 122 | dict[d.id] = line; |
||
| 123 | |||
| 124 | return line; |
||
| 125 | }); |
||
| 126 | } |
||
| 127 | |||
| 128 | return L.GridLayer.extend({ |
||
| 129 | onAdd: function (map) { |
||
| 130 | L.GridLayer.prototype.onAdd.call(this, map); |
||
| 131 | if (this.data) { |
||
| 132 | this.prepareLabels(); |
||
| 133 | } |
||
| 134 | }, |
||
| 135 | setData: function (data, map, nodeDict, linkDict, linkScale, router, config) { |
||
| 136 | // Check if init or data is already set |
||
| 137 | if (groupLines) { |
||
| 138 | groupOffline.clearLayers(); |
||
| 139 | groupOnline.clearLayers(); |
||
| 140 | groupNew.clearLayers(); |
||
| 141 | groupLost.clearLayers(); |
||
| 142 | groupLines.clearLayers(); |
||
| 143 | } |
||
| 144 | |||
| 145 | var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router); |
||
| 146 | groupLines = L.featureGroup(lines).addTo(map); |
||
| 147 | |||
| 148 | var nodesOnline = helper.subtract(data.nodes.all.filter(helper.online), data.nodes.new); |
||
| 149 | var nodesOffline = helper.subtract(data.nodes.all.filter(helper.offline), data.nodes.lost); |
||
| 150 | |||
| 151 | var markersOnline = nodesOnline.filter(helper.hasLocation) |
||
| 152 | .map(mkMarker(nodeDict, function () { |
||
| 153 | return Object.assign({}, config.icon.base, config.icon.online); |
||
| 154 | }, router)); |
||
| 155 | |||
| 156 | var markersOffline = nodesOffline.filter(helper.hasLocation) |
||
| 157 | .map(mkMarker(nodeDict, function () { |
||
| 158 | return Object.assign({}, config.icon.base, config.icon.offline); |
||
| 159 | }, router)); |
||
| 160 | |||
| 161 | var markersNew = data.nodes.new.filter(helper.hasLocation) |
||
| 162 | .map(mkMarker(nodeDict, function () { |
||
| 163 | return Object.assign({}, config.icon.base, config.icon.new); |
||
| 164 | }, router)); |
||
| 165 | |||
| 166 | var markersLost = data.nodes.lost.filter(helper.hasLocation) |
||
| 167 | .map(mkMarker(nodeDict, function (d) { |
||
| 168 | if (d.lastseen.isAfter(moment(data.now).subtract(config.maxAgeAlert, 'days'))) { |
||
| 169 | return Object.assign({}, config.icon.base, config.icon.alert); |
||
| 170 | } |
||
| 171 | |||
| 172 | if (d.lastseen.isAfter(moment(data.now).subtract(config.maxAge, 'days'))) { |
||
| 173 | return Object.assign({}, config.icon.base, config.icon.lost); |
||
| 174 | } |
||
| 175 | return null; |
||
| 176 | }, router)); |
||
| 177 | |||
| 178 | groupOffline = L.featureGroup(markersOffline).addTo(map); |
||
| 179 | groupLost = L.featureGroup(markersLost).addTo(map); |
||
| 180 | groupOnline = L.featureGroup(markersOnline).addTo(map); |
||
| 181 | groupNew = L.featureGroup(markersNew).addTo(map); |
||
| 182 | |||
| 183 | this.data = { |
||
| 184 | online: nodesOnline.filter(helper.hasLocation), |
||
| 185 | offline: nodesOffline.filter(helper.hasLocation), |
||
| 186 | new: data.nodes.new.filter(helper.hasLocation), |
||
| 187 | lost: data.nodes.lost.filter(helper.hasLocation) |
||
| 188 | }; |
||
| 189 | this.updateLayer(); |
||
| 190 | }, |
||
| 191 | updateLayer: function () { |
||
| 192 | if (this._map) { |
||
| 193 | this.prepareLabels(); |
||
| 194 | } |
||
| 195 | }, |
||
| 196 | prepareLabels: function () { |
||
| 197 | var d = this.data; |
||
| 198 | |||
| 199 | // label: |
||
| 200 | // - position (WGS84 coords) |
||
| 201 | // - offset (2D vector in pixels) |
||
| 202 | // - anchor (tuple, textAlignment, textBaseline) |
||
| 203 | // - minZoom (inclusive) |
||
| 204 | // - label (string) |
||
| 205 | // - color (string) |
||
| 206 | |||
| 207 | var labelsOnline = d.online.map(prepareLabel(null, 11, 8, true)); |
||
| 208 | var labelsOffline = d.offline.map(prepareLabel('rgba(212, 62, 42, 0.9)', 9, 5, false)); |
||
| 209 | var labelsNew = d.new.map(prepareLabel('rgba(48, 99, 20, 0.9)', 11, 8, true)); |
||
| 210 | var labelsLost = d.lost.map(prepareLabel('rgba(212, 62, 42, 0.9)', 11, 8, true)); |
||
| 211 | |||
| 212 | var labels = [] |
||
| 213 | .concat(labelsNew) |
||
| 214 | .concat(labelsLost) |
||
| 215 | .concat(labelsOnline) |
||
| 216 | .concat(labelsOffline); |
||
| 217 | |||
| 218 | var minZoom = this.options.minZoom; |
||
| 219 | var maxZoom = this.options.maxZoom; |
||
| 220 | |||
| 221 | var trees = []; |
||
| 222 | |||
| 223 | var map = this._map; |
||
| 224 | |||
| 225 | function nodeToRect(z) { |
||
| 226 | return function (n) { |
||
| 227 | var p = map.project(n.position, z); |
||
| 228 | return { minX: p.x - nodeRadius, minY: p.y - nodeRadius, maxX: p.x + nodeRadius, maxY: p.y + nodeRadius }; |
||
| 229 | }; |
||
| 230 | } |
||
| 231 | |||
| 232 | for (var z = minZoom; z <= maxZoom; z++) { |
||
| 233 | trees[z] = rbush(9); |
||
| 234 | trees[z].load(labels.map(nodeToRect(z))); |
||
| 235 | } |
||
| 236 | |||
| 237 | labels = labels.map(function (n) { |
||
| 238 | var best = labelLocations.map(function (loc) { |
||
| 239 | var offset = calcOffset(n.offset, loc); |
||
| 240 | var i; |
||
| 241 | |||
| 242 | for (i = maxZoom; i >= minZoom; i--) { |
||
| 243 | var p = map.project(n.position, i); |
||
| 244 | var rect = labelRect(p, offset, loc, n, minZoom, maxZoom, i); |
||
| 245 | var candidates = trees[i].search(rect); |
||
| 246 | |||
| 247 | if (candidates.length > 0) { |
||
| 248 | break; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | return { loc: loc, z: i + 1 }; |
||
| 253 | }).filter(function (k) { |
||
| 254 | return k.z <= maxZoom; |
||
| 255 | }).sort(function (a, b) { |
||
| 256 | return a.z - b.z; |
||
| 257 | })[0]; |
||
| 258 | |||
| 259 | if (best !== undefined) { |
||
| 260 | n.offset = calcOffset(n.offset, best.loc); |
||
| 261 | n.minZoom = best.z; |
||
| 262 | n.anchor = best.loc; |
||
| 263 | |||
| 264 | for (var i = maxZoom; i >= best.z; i--) { |
||
| 265 | var p = map.project(n.position, i); |
||
| 266 | var rect = labelRect(p, n.offset, best.loc, n, minZoom, maxZoom, i); |
||
| 267 | trees[i].insert(rect); |
||
| 268 | } |
||
| 269 | |||
| 270 | return n; |
||
| 271 | } |
||
| 272 | return undefined; |
||
| 273 | }).filter(function (n) { |
||
| 274 | return n !== undefined; |
||
| 275 | }); |
||
| 276 | |||
| 277 | this.margin = 16; |
||
| 278 | |||
| 279 | if (labels.length > 0) { |
||
| 280 | this.margin += labels.map(function (n) { |
||
| 281 | return n.width; |
||
| 282 | }).sort().reverse()[0]; |
||
| 283 | } |
||
| 284 | |||
| 285 | this.labels = rbush(9); |
||
| 286 | this.labels.load(labels.map(mapRTree)); |
||
| 287 | |||
| 288 | this.redraw(); |
||
| 289 | }, |
||
| 290 | createTile: function (tilePoint) { |
||
| 291 | var tile = L.DomUtil.create('canvas', 'leaflet-tile'); |
||
| 292 | |||
| 293 | var tileSize = this.options.tileSize; |
||
| 294 | tile.width = tileSize; |
||
| 295 | tile.height = tileSize; |
||
| 296 | |||
| 297 | if (!this.labels) { |
||
| 298 | return tile; |
||
| 299 | } |
||
| 300 | |||
| 301 | var s = tilePoint.multiplyBy(tileSize); |
||
| 302 | var map = this._map; |
||
| 303 | bodyStyle = window.getComputedStyle(document.querySelector('body')); |
||
| 304 | labelShadow = bodyStyle.backgroundColor.replace(/rgb/i, 'rgba').replace(/\)/i, ',0.7)'); |
||
| 305 | |||
| 306 | function projectNodes(d) { |
||
| 307 | var p = map.project(d.label.position); |
||
| 308 | |||
| 309 | p.x -= s.x; |
||
| 310 | p.y -= s.y; |
||
| 311 | |||
| 312 | return { p: p, label: d.label }; |
||
| 313 | } |
||
| 314 | |||
| 315 | var bbox = helper.getTileBBox(s, map, tileSize, this.margin); |
||
| 316 | var labels = this.labels.search(bbox).map(projectNodes); |
||
| 317 | var ctx = tile.getContext('2d'); |
||
| 318 | |||
| 319 | ctx.lineWidth = 5; |
||
| 320 | ctx.strokeStyle = labelShadow; |
||
| 321 | ctx.miterLimit = 2; |
||
| 322 | |||
| 323 | function drawLabel(d) { |
||
| 324 | ctx.font = d.label.font; |
||
| 325 | ctx.textAlign = d.label.anchor[0]; |
||
| 326 | ctx.textBaseline = d.label.anchor[1]; |
||
| 327 | ctx.fillStyle = d.label.fillStyle === null ? bodyStyle.color : d.label.fillStyle; |
||
| 328 | |||
| 329 | if (d.label.stroke) { |
||
| 330 | ctx.strokeText(d.label.label, d.p.x + d.label.offset[0], d.p.y + d.label.offset[1]); |
||
| 331 | } |
||
| 332 | |||
| 333 | ctx.fillText(d.label.label, d.p.x + d.label.offset[0], d.p.y + d.label.offset[1]); |
||
| 334 | } |
||
| 335 | |||
| 336 | labels.filter(function (d) { |
||
| 337 | return tilePoint.z >= d.label.minZoom; |
||
| 338 | }).forEach(drawLabel); |
||
| 339 | |||
| 340 | return tile; |
||
| 341 | } |
||
| 342 | }); |
||
| 343 | }); |
||
| 344 |